#ifndef _STRINGUTILS_H
#define _STRINGUTILS_H

#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <vector>
#include <windows.h>

#ifdef GALLIUMENGINE_DLL 
#define GALLIUMENGINE_API __declspec( dllexport )
#else
#define GALLIUMENGINE_API __declspec( dllimport )
#endif

namespace GalliumEngine
{

	class GALLIUMENGINE_API StringUtils
	{
	public:
		 static std::string format(const char *format, ...)
		 {
			 std::string fstring("");

			if (NULL != format)
			{
				va_list marker = NULL; 
				va_start(marker, format); 
				size_t len = _vscprintf(format, marker) + 1;
				std::vector<char> buffer(len, '\0');
				int nWritten = _vsnprintf_s(&buffer[0], buffer.size(), len, format, marker);    
				if (nWritten > 0) { fstring = &buffer[0]; }    
				va_end(marker); 
			}

			return fstring;
		 }

		template <typename T>
		static bool fromString(T& t, 
						 const std::string& s, 
						 std::ios_base& (*f)(std::ios_base&))
		{
			std::istringstream iss(s);
			return !(iss >> f >> t).fail();
		}

		static void split(std::string line, char c, std::vector<std::string>& out)
		{
			std::string segment = "";

			for(unsigned int i = 0; i < line.length(); ++i)
			{
				if(line[i] != c)
				{
					segment += line[i];
				}
				else
				{
					if(segment.length() > 0)
					{
						out.push_back(segment);
						segment = "";
					}
				}
			}

			out.push_back(segment);
		}
	};

}

#endif